import { PlaneEvent } from "../Constant/Constant";
import EnemyControl from "../Control/EnemyControl";
import { Random } from "../Utils/Random";
import PoolManager from "./PoolManager";

const { ccclass, property } = cc._decorator;

export type DiedCallback = (score: number) => void

/**
 * 敌机生成控制类
 * 控制不同敌机的生成和敌机Combo
 */
@ccclass
export default class EnemyManager {
  private enemy1Prefab: cc.Prefab = null
  private enemy2Prefab: cc.Prefab = null
  private enemy3Prefab: cc.Prefab = null
  private enemyParent: cc.Node = null

  private enemyDiedCallback: Function | DiedCallback = null

  /**
   * 初始化敌机控制器
   * @param enemy1Prefab 敌机1预制
   * @param enemy2Prefab 敌机2预制
   * @param enemy3Prefab 敌机3预制
   * @param parent 设置敌机父节点，默认为当前场景
   */
  public init(enemy1Prefab: cc.Prefab, enemy2Prefab: cc.Prefab, enemy3Prefab: cc.Prefab, parent: cc.Node = cc.director.getScene()): void {
    this.enemy1Prefab = enemy1Prefab
    this.enemy2Prefab = enemy2Prefab
    this.enemy3Prefab = enemy3Prefab
    this.enemyParent = parent
  }

  /**
   * 随机创建敌机1
   */
  public randomCreateEnemy1(): void {
    let enemy1Node = PoolManager.instance.getNode(this.enemy1Prefab)
    enemy1Node.parent = this.enemyParent
    enemy1Node.getComponent(EnemyControl).init()
    // 设置坐标
    // x坐标范围：25 - 453
    // y坐标 820
    let randomNum = Random.getIntNum(25, 454)
    enemy1Node.setPosition(randomNum, 820)
  }

  /**
   * 随机创建敌机2
   */
  public randomCreateEnemy2(): void {
    let enemy2Node = PoolManager.instance.getNode(this.enemy2Prefab)
    enemy2Node.getComponent(EnemyControl).init()
    enemy2Node.parent = this.enemyParent
    // 设置坐标
    // x坐标范围：35 - 444
    // y坐标：844
    let randomNum = Random.getIntNum(35, 444)
    enemy2Node.setPosition(randomNum, 844)
  }

  /**
   * 随机创建敌机3
   */
  public randomCreateEnemy3(): void {
    let enemy3Node = PoolManager.instance.getNode(this.enemy3Prefab)
    enemy3Node.getComponent(EnemyControl).init()
    enemy3Node.parent = this.enemyParent
    // 设置坐标
    // x坐标范围：56 - 420
    // y坐标：886
    let randomNum = Random.getIntNum(56, 420)
    enemy3Node.setPosition(randomNum, 886)
  }

  /**
   * 敌机组合1
   * 组合形式: 全小型 V字排开 无攻击
   */
  public showComb1(): void {
    // 生成7架敌机，每架敌机间隔20
    const beginX = 27
    const max = 7
    for (let i = 0; i < max; i++) {
      const enemy = PoolManager.instance.getNode(this.enemy1Prefab)
      enemy.getComponent(EnemyControl).init()
      enemy.parent = this.enemyParent
      // X轴间隔
      enemy.x = beginX + i * (enemy.width + 20)
      // Y轴间隔
      const middle = Math.floor(max / 2)
      if (i <= middle) {
        enemy.y = 820 + (middle - i) * 30
      } else {
        enemy.y = 820 - (middle - i) * 30
      }
    }
  }

  /**
   * 敌机组合2
   * 组合形式: 全中性 2行2列 会发射一颗子弹
   */
  public showComb2(): void {
    console.log('showComb2')
    const positions: cc.Vec2[] = [
      new cc.Vec2(100, 844),
      new cc.Vec2(180, 930),
      new cc.Vec2(300, 930),
      new cc.Vec2(380, 844)
    ]

    for (let i = 0; i < 4; i++) {
      const enemy = PoolManager.instance.getNode(this.enemy2Prefab)
      enemy.getComponent(EnemyControl).init()
      enemy.parent = this.enemyParent
      enemy.setPosition(positions[i])
    }
  }

  /**
   * 敌机组合3
   * 组合形式 全大型 2架一字排开 一直发射子弹
   */
  public showComb3(): void {
    console.log('showComb3')
    const positions: cc.Vec2[] = [
      new cc.Vec2(130, 886),
      new cc.Vec2(350, 886),
    ]

    for (let i = 0; i < 2; i++) {
      const enemy = PoolManager.instance.getNode(this.enemy3Prefab)
      enemy.getComponent(EnemyControl).init()
      enemy.parent = this.enemyParent
      enemy.setPosition(positions[i])
    }
  }

  // /**
  //  * 敌机死亡监听器
  //  * @param callback 回调函数，score参数获取得分
  //  */
  // public enemyDiedListener(callback: Function | DiedCallback): void {
  //   this.enemyParent.on(PlaneEvent.DIED, (event: cc.Event.EventCustom) => {
  //     const score = event.getUserData().score
  //     if (callback != null) {
  //       callback(score)
  //     }
  //     event.stopPropagation()
  //   }) // XXX:off event
  // }

  /**
   * 设置敌机销毁的回调函数
   * @param callback 
   */
  public setEnemyDiedCallback(callback: Function | DiedCallback): void {
    if (this.enemyDiedCallback != null) {
      this.enemyParent.off(PlaneEvent.DIED, this.enemyDiedCallback)
    }
    this.enemyDiedCallback = (event: cc.Event.EventCustom) => {
      const score = event.getUserData().score
      if (callback != null) {
        callback(score)
      }
      event.stopPropagation()
    }
    this.enemyParent.on(PlaneEvent.DIED, this.enemyDiedCallback)
  }


  /**
   * 销毁所有敌机
   */
  public destroyAllNode() {
    this.enemyParent.children.forEach((node: cc.Node) => {
      node.destroy()
    })
  }

  /**
   * 销毁所有敌机，并且会显示销毁效果
   */
  public destoryAllNodeCallDie() {
    this.enemyParent.children.forEach((node: cc.Node) => {
      const enemyCtrl = node.getComponent(EnemyControl)
      enemyCtrl?.die()
    })
  }
}
